package com.gxa.saas.controller;

        import com.gxa.saas.pojo.Vendor;
        import com.gxa.saas.service.VendorService;
        import com.gxa.saas.utils.JsonResult;
        import io.swagger.annotations.ApiOperation;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.stereotype.Controller;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.bind.annotation.RequestMethod;
        import org.springframework.web.bind.annotation.RequestParam;
        import org.springframework.web.bind.annotation.ResponseBody;


        import java.text.SimpleDateFormat;
        import java.util.Date;
        import java.util.HashMap;
        import java.util.List;
        import java.util.Map;

@Controller
@ResponseBody
@RequestMapping("/vendor")
public class VendorController {
    @Autowired
    VendorService vendorService;

    @ApiOperation("分页查询用户信息的方法")
    @RequestMapping(value = "/selectByPagevendor", method = {RequestMethod.POST})
    public Map selectByPage(@RequestParam(value = "page") Integer page,
                            @RequestParam(value = "limit") Integer limit) {
        List<Vendor> vendors = vendorService.selectByPage(page, limit);
        List<Vendor> vendors1 = vendorService.selectAll();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.set("totalCount",vendors1.size());
        jsonResult.setData(vendors);
        return jsonResult.getMap();
    }
    @ApiOperation("添加用户信息的方法")
    @RequestMapping(value = "/insertvendor",method ={RequestMethod.POST})
    public Map insert(Vendor vendor){
        Integer inert = vendorService.inert(vendor);
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
        Date date = new Date(System.currentTimeMillis());
        System.out.println(formatter.format(date));
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("添加成功");
        jsonResult.setData(inert);
        return jsonResult.getMap();
    }
    @ApiOperation("修改用户信息的方法")
    @RequestMapping(value = "/updatevendor",method ={RequestMethod.POST})
    public Map update(Vendor vendor){
        vendorService.update(vendor);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("添加成功");
        return jsonResult.getMap();
    }
    @ApiOperation("删除用户信息的方法")
    @RequestMapping(value = "/deletevendor",method ={RequestMethod.POST})
    public Map delete(Integer id){
        vendorService.delete(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("删除成功");
        return jsonResult.getMap();
    }
    @ApiOperation("用多个字段查询用户信息的方法")
    @RequestMapping(value = "/selectByNameOrContactpersonvendor",method ={RequestMethod.POST})
    public Map selectByNameOrContactpersonOrCreatypersonOrLeaderperson(
            @RequestParam(value = "ve_name",required = false)String ve_name,
            @RequestParam(value = "ve_contact",required = false)String ve_contact){
        Map map = new HashMap<>();
        map.put("ve_name",ve_name);
        map.put("ve_contact",ve_contact);
        List<Vendor> vendors = vendorService.selectByNameOrContactperson(map);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.set("totalCount",vendors.size());
        jsonResult.setData(vendors);
        return jsonResult.getMap();
    }
    @ApiOperation("全部查询用户信息的方法")
    @RequestMapping(value = "/selectAllvendor", method = {RequestMethod.POST})
    public Map selectAll() {
        List<Vendor> vendors = vendorService.selectAll();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(vendors);
        return jsonResult.getMap();
    }
}
